package com.app.shortvideo.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.app.shortvideo.R
import com.app.library.ext.visible
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.SizeUtils

class EmptyCustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    var callback: EmptyCustomCallback? = null
    init {
        initComponents(context, attrs, defStyleAttr)
    }
    var tvTitle:TextView? = null
    var tvDescribe:TextView? = null
    private fun initComponents(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        setPadding(SizeUtils.dp2px(8f), 0, SizeUtils.dp2px(8f), 0)
        inflate(context, R.layout.layout_empty_custom, this)
        orientation = HORIZONTAL

        val attrs = context.obtainStyledAttributes(
            attrs,
            R.styleable.EmptyCustomView, defStyleAttr, 0
        )
        val icon: Drawable? = attrs.getDrawable(R.styleable.EmptyCustomView_empty_icon)
        val title: String? = attrs.getString(R.styleable.EmptyCustomView_empty_title)
        val describe: String? = attrs.getString(R.styleable.EmptyCustomView_empty_describe)
        val actionText: String? = attrs.getString(R.styleable.EmptyCustomView_empty_actionText)
        val gravity: String? = attrs.getString(R.styleable.EmptyCustomView_empty_gravity)

        val layoutEmpty:LinearLayout = findViewById(R.id.layoutEmpty)
        val btnAction:TextView = findViewById(R.id.btnAction)
        tvTitle = findViewById(R.id.tvTitle)
        tvDescribe = findViewById(R.id.tvDescribe)
        val ivIcon:ImageView = findViewById(R.id.ivIcon)
        gravity?.let {
            val la = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)
            if (it=="center"){
                la.gravity = Gravity.CENTER
            }else if(it=="center_horizontal"){
                layoutEmpty.setPadding(16,100,16,100)
                la.gravity = Gravity.CENTER_VERTICAL
            }
            layoutEmpty.layoutParams = la
        }
        actionText?.let {
            btnAction.visible(true)
            btnAction.text = actionText
        }
        icon?.let {
            ivIcon.visible(true)
            ivIcon.setImageDrawable(icon)
        }
        title?.let {
            tvTitle?.text = title
        }
        describe?.let {
            tvDescribe?.text = describe
        }
        btnAction.setOnClickListener {
            callback?.onAction()
        }
    }

    fun setTitle(title:String){
        tvTitle?.text = title
    }
    fun setDescribe(describe:String){
        tvDescribe?.text = describe
    }

    fun refreshVisible(dataSize:Int){
        if (dataSize==0){
            visible(true)
        }else{
            false
        }
    }
    fun setCallBack(callback: EmptyCustomCallback) {
        this.callback =callback
    }

    interface EmptyCustomCallback{
        fun onAction()
    }

    fun hasNet(): Boolean {
        if (NetworkUtils.getNetworkType() == NetworkUtils.NetworkType.NETWORK_NO) {
            return false
        }
        return true
    }

    fun refreshState() {
        visible(!hasNet())
    }

    interface EmptyNetCallback {
        fun onRetry()
    }
}